import { PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
export const userResolvers = {
    query: {
        users: async () => {
            return prisma.user.findMany({
                include: { school: true },
            });
        },
        user: async (_: any, { id }: { id: string }) => {
            return prisma.user.findUnique({
                where: { id },
                include: { school: true },
            });
        },
    },
    mutation: {
        createUser: async (_: any, { name, email, schoolId }: { name: string, email: string, schoolId: string }) => {
            // const existingUser = await prisma.user.findUnique({ where: { name } });
            // if (existingUser) {
            //     throw new Error('用户名已存在');
            // }
            return prisma.user.create({
                data: { name, email, schoolId },
                include: { school: true },
            });
        },
        updateUser: async (_: any, { id, name, email, schoolId }: { id: string, name?: string, email?: string, schoolId?: string }) => {
            // if (name) {
            //     const existingUser = await prisma.user.findFirst({ where: { name, NOT: { id } } });
            //     if (existingUser) {
            //         throw new Error('用户名已存在');
            //     }
            // }
            return prisma.user.update({
                where: { id },
                data: { name, email, schoolId },
                include: { school: true },
            });
        },
        deleteUser: async (_: any, { id }: { id: string }) => {
            return prisma.user.delete({
                where: { id },
                include: { school: true },
            });


        },
    },
    join: {
        School: {
            users: async (_: { id: string }) => {
                return prisma.user.findMany({
                    where: { schoolId: _.id } as any,
                });
            },
        },

    }

}